
#include "stdafx.h"
#include "WicBitmapEncoder.h"
#include "WicBitmapFrameEncode.h"

//==============================================================================
// CWicBitmapFrameEncode
//==============================================================================

CWicBitmapFrameEncode::CWicBitmapFrameEncode(
    CWicBitmapEncoder *pIWICBitmapEncoder, IPropertyBag2 **ppIEncoderOptions)
{
    m_pIWICBitmapFrameEncode = NULL;
    HRESULT hr = pIWICBitmapEncoder->CreateNewFrame(&m_pIWICBitmapFrameEncode, ppIEncoderOptions);
    m_pIWICBitmapFrameEncode->Initialize(*ppIEncoderOptions);
    hr = S_OK;
}

CWicBitmapFrameEncode::~CWicBitmapFrameEncode()
{
    if(m_pIWICBitmapFrameEncode)
    {
        m_pIWICBitmapFrameEncode->Release();
    }
    m_pIWICBitmapFrameEncode = NULL;
}

CWicBitmapFrameEncode::CWicBitmapFrameEncode(CWicBitmapFrameEncode const & objBitmapFrameEncode)
{
    m_pIWICBitmapFrameEncode = objBitmapFrameEncode.m_pIWICBitmapFrameEncode;
    m_pIWICBitmapFrameEncode->AddRef();
}

CWicBitmapFrameEncode& CWicBitmapFrameEncode::operator=(CWicBitmapFrameEncode const & objBitmapFrameEncode)
{
    if(m_pIWICBitmapFrameEncode)
    {
        m_pIWICBitmapFrameEncode->Release();
    }
    m_pIWICBitmapFrameEncode = objBitmapFrameEncode.m_pIWICBitmapFrameEncode;
    m_pIWICBitmapFrameEncode->AddRef();
    return *this;
}

//==============================================================================
// IWICBitmapFrameEncode
//==============================================================================
HRESULT STDMETHODCALLTYPE CWicBitmapFrameEncode::Initialize( 
    /* [unique][in] */ __RPC__in_opt IPropertyBag2 *pIEncoderOptions)
{
    return m_pIWICBitmapFrameEncode->Initialize(pIEncoderOptions);
}

HRESULT STDMETHODCALLTYPE CWicBitmapFrameEncode::SetSize( 
    /* [in] */ UINT uiWidth,
    /* [in] */ UINT uiHeight)
{
    return m_pIWICBitmapFrameEncode->SetSize(uiWidth, uiHeight);
}

HRESULT STDMETHODCALLTYPE CWicBitmapFrameEncode::SetResolution( 
    /* [in] */ double dpiX,
    /* [in] */ double dpiY)
{
    return m_pIWICBitmapFrameEncode->SetResolution(dpiX, dpiY);
}

HRESULT STDMETHODCALLTYPE CWicBitmapFrameEncode::SetPixelFormat( 
    /* [out][in] */ __RPC__inout WICPixelFormatGUID *pPixelFormat)
{
    return m_pIWICBitmapFrameEncode->SetPixelFormat(pPixelFormat);
}

HRESULT STDMETHODCALLTYPE CWicBitmapFrameEncode::SetColorContexts( 
    /* [in] */ UINT cCount,
    /* [size_is][in] */ __RPC__in_ecount_full(cCount) IWICColorContext **ppIColorContext)
{
    return m_pIWICBitmapFrameEncode->SetColorContexts(cCount, ppIColorContext);
}

HRESULT STDMETHODCALLTYPE CWicBitmapFrameEncode::SetPalette( 
    /* [in] */ __RPC__in_opt CWicPalette *pIPalette)
{
    return m_pIWICBitmapFrameEncode->SetPalette(pIPalette->GetPtr());
}

HRESULT STDMETHODCALLTYPE CWicBitmapFrameEncode::SetThumbnail( 
    /* [in] */ __RPC__in_opt IWICBitmapSource *pIThumbnail)
{
    return m_pIWICBitmapFrameEncode->SetThumbnail(pIThumbnail);
}

HRESULT STDMETHODCALLTYPE CWicBitmapFrameEncode::WritePixels( 
    /* [in] */ UINT lineCount,
    /* [in] */ UINT cbStride,
    /* [in] */ UINT cbBufferSize,
    /* [size_is][in] */ __RPC__in_ecount_full(cbBufferSize) BYTE *pbPixels)
{
    return m_pIWICBitmapFrameEncode->WritePixels(lineCount, cbStride, cbBufferSize, pbPixels);
}

HRESULT STDMETHODCALLTYPE CWicBitmapFrameEncode::WriteSource( 
    /* [in] */ __RPC__in_opt CWicBitmapSource *pIBitmapSource,
    /* [unique][in] */ __RPC__in_opt WICRect *pRect)
{
    if(pRect)
    {
        return m_pIWICBitmapFrameEncode->WriteSource(pIBitmapSource->GetPtr(), pRect);
    }
    else
    {
        IWICBitmapSource* ptr = pIBitmapSource->GetPtr();
        UINT uWidth, uHeight;
        ptr->GetSize(&uWidth, &uHeight);
        WICRect rect = {0, 0, uWidth, uHeight};
        return m_pIWICBitmapFrameEncode->WriteSource(ptr, &rect);
    }
}

HRESULT STDMETHODCALLTYPE CWicBitmapFrameEncode::WriteSource( 
    /* [in] */ __RPC__in_opt IWICBitmapSource *pIBitmapSource,
    /* [unique][in] */ __RPC__in_opt WICRect *pRect)
{
    if(pRect)
    {
        return m_pIWICBitmapFrameEncode->WriteSource(pIBitmapSource, pRect);
    }
    else
    {
        UINT uWidth, uHeight;
        pIBitmapSource->GetSize(&uWidth, &uHeight);
        WICRect rect = {0, 0, uWidth, uHeight};
        return m_pIWICBitmapFrameEncode->WriteSource(pIBitmapSource, &rect);
    }
}

HRESULT STDMETHODCALLTYPE CWicBitmapFrameEncode::Commit()
{
    return m_pIWICBitmapFrameEncode->Commit();
}

HRESULT STDMETHODCALLTYPE CWicBitmapFrameEncode::GetMetadataQueryWriter( 
    /* [out] */ __RPC__deref_out_opt IWICMetadataQueryWriter **ppIMetadataQueryWriter)
{
    return m_pIWICBitmapFrameEncode->GetMetadataQueryWriter(ppIMetadataQueryWriter);
}





